<?php

namespace app\data\controller\rework;

use app\admin\service\FeiShuService;
use app\admin\service\WxWorkService;
use think\admin\Controller;

/**
 * 品质解锁
 * Class ReworkRecord
 * @package app\data\controller\rework
 */
class ReworkRecord extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'rework_record';

    /**
     * 品质解锁管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '品质解锁管理';
        $query = $this->_query($this->table);
        $query->equal('uuid_city,is_unlock')->like('plate_number');

        $uuidCityName = $this->request->request('uuid_city_name', '');
        if (!empty($uuidCityName)) {
            $uuidCityIds = $this->app->db->name('work_user')->where('name', 'like', "%{$uuidCityName}%")->column('id');
            $query->whereIn('uuid_city', $uuidCityIds);
        }

        $query->order('id desc');

        // 数据查询分页
        if (input('output') == 'json') {
            $result = $query->page(true, false);
            $this->success('', $result);
        } else {
            $query->page();
        }
    }

    /**
     * 品质解锁导出
     * @auth true
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function indexExport()
    {
        $this->index();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _indexExport_page_filter(array &$data)
    {
        $this->_index_page_filter($data);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        $uuidCity = array_column($data, 'uuid_city');

        $workUsers = $this->app->db->name('work_user')->whereIn('id', $uuidCity)->select()->toArray();
        foreach ($workUsers as $workUser) {
            $userId2Name[$workUser['id']] = $workUser['name'];
        }

        foreach ($data as &$item) {
            $item['uuid_city_name'] = $userId2Name[$item['uuid_city']] ?? '';
            $item['is_unlock_text'] = $item['is_unlock'] ? '是' : '否';
            $item['images'] = explode('|', $item['images'] ?: '');
        }
    }

    /**
     * 解锁
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function unlock()
    {
        $id = $this->request->request('id');

        $rewordRecord = $this->app->db->name($this->table)->where('id', $id)->findOrEmpty();
        if (empty($rewordRecord)) {
            $this->error('查无该记录');
        }

        if ($rewordRecord['is_unlock']) {
            $this->error('已经解锁了');
        }

        $this->app->db->name($this->table)->where('id', $id)->update(['is_unlock' => 1, 'unlock_at' => date('Y-m-d H:i:s')]);

        $openId = $this->app->db->name('work_user')->where('id', $rewordRecord['uuid_city'])->value('open_id');
        if ($openId) {
            $messageText = '尊敬的师傅，您已解锁成功，请遵守品质要求，避免再次被锁:打卡后即可开始接单啦。';
            FeiShuService::instance()->sendMessageTextByUserid($openId, $messageText);
        }

        $this->success('解锁成功');
    }
}